package com.zHan.system.controller.system;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zHan.common.result.Result;
import com.zHan.model.dto.AssginPostDto;
import com.zHan.model.dto.SysPostQueryDto;
import com.zHan.model.system.SysPost;
import com.zHan.system.anno.Log;
import com.zHan.system.menu.BusinessType;
import com.zHan.system.service.ISysPostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 岗位信息表 前端控制器
 * </p>
 *
 * @author zHan
 * @since 2023-06-24
 */
@RestController
@Slf4j
@RequestMapping("/admin/system/sysPost")
@Api(tags = "岗位管理接口")
public class SysPostController {

    @Autowired
    private ISysPostService sysPostService;

    /**
     * 新增岗位
     *
     * @param sysPost
     * @return
     */
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('bnt.sysPost.add')")
    @ApiOperation(value = "新增岗位")
    @Log(title = "岗位管理", businessType = BusinessType.INSERT)
    public Result save(@RequestBody SysPost sysPost) {
        boolean isSuccess = sysPostService.save(sysPost);
        if (isSuccess) {
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 删除岗位
     *
     * @param id
     * @return
     */
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("hasAuthority('bnt.sysPost.remove')")
    @ApiOperation(value = "删除岗位")
    @Log(title = "岗位管理", businessType = BusinessType.DELETE)
    public Result remove(@PathVariable Long id) {
        boolean isSuccess = sysPostService.removePostById(id);
        if (isSuccess) {
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 批量删除岗位
     *
     * @param idList
     * @return
     */
    @DeleteMapping("/batchRemove")
    @PreAuthorize("hasAuthority('bnt.sysPost.remove')")
    @ApiOperation(value = "批量删除岗位")
    @Log(title = "岗位管理", businessType = BusinessType.DELETE)
    public Result batchRemove(@RequestBody List<Long> idList) {
        boolean isSuccess = sysPostService.removePostByIds(idList);
        if (isSuccess) {
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 修改岗位
     *
     * @param sysPost
     * @return
     */
    @PutMapping("/update")
    @PreAuthorize("hasAuthority('bnt.sysPost.update')")
    @ApiOperation(value = "修改岗位")
    @Log(title = "岗位管理", businessType = BusinessType.UPDATE)
    public Result update(@RequestBody SysPost sysPost) {
        boolean isSuccess = sysPostService.updateById(sysPost);
        if (isSuccess) {
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 根据id查询单个岗位
     *
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    @ApiOperation(value = "查询单个岗位")
    public Result findPost(@PathVariable String id) {
        SysPost post = sysPostService.getById(id);
        if (post != null) {
            return Result.ok(post);
        }
        return Result.ok('空');
    }

    /**
     * 分页条件查询
     *
     * @param page
     * @param pageSize
     * @param sysPostQueryDto
     * @return
     */
    @GetMapping("/{page}/{pageSize}")
    @PreAuthorize("hasAuthority('bnt.sysPost.list')")
    @ApiOperation(value = "岗位分页条件查询")
    public Result findPageQueryUser(@ApiParam(name = "page", value = "当前页码", required = true)
                                    @PathVariable Long page,
                                    @ApiParam(name = "pageSize", value = "每页记录数", required = true)
                                    @PathVariable Long pageSize,
                                    @ApiParam(name = "roleQueryVo", value = "查询对象")
                                    SysPostQueryDto sysPostQueryDto) {
        Page<SysPost> pageInfo = sysPostService.selectPageQuery(page, pageSize, sysPostQueryDto);
        //返回
        return Result.ok(pageInfo);
    }

    /**
     * 更改岗位状态
     *
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/updateStatus/{id}/{status}")
    @PreAuthorize("hasAuthority('bnt.sysPost.update')")
    @ApiOperation(value = "更改岗位状态")
    @Log(title = "岗位管理", businessType = BusinessType.STATUS)
    public Result updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        boolean isSuccess = sysPostService.updateStatus(id, status);
        if (isSuccess) {
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 获取用户的岗位数据
     *
     * @param userId
     * @return
     */
    @GetMapping("/toAssign/{userId}")
    @ApiOperation(value = "获取用户的岗位数据")
    public Result toAssign(@PathVariable String userId) {
        Map<String, Object> map = sysPostService.selectPostsByUserId(userId);
        if (!map.isEmpty()) {
            return Result.ok(map);
        }
        return Result.fail();
    }

    /**
     * 给用户分配岗位
     *
     * @param assginPostDto
     * @return
     */
    @PostMapping("/doAssign")
    @ApiOperation(value = "给用户分配岗位")
    @PreAuthorize("hasAuthority('bnt.sysUser.assignPost')")
    @Log(title = "岗位管理", businessType = BusinessType.OTHER)
    public Result doAssign(@RequestBody AssginPostDto assginPostDto) {
        boolean isSuccess = sysPostService.assignPostToUser(assginPostDto);
        if (isSuccess) {
            return Result.ok();
        }
        return Result.fail();
    }
}

